<!DOCTYPE html>
<html>
<head>
	<title>类目列表</title>
	<meta charset="utf-8"/>
	<link rel="stylesheet" href="../../css/bootstrap.css"/>
	<script type="text/javascript" src="../../js/jquery-3.1.1.min.js"></script>
	<script type="text/javascript" src="../../js/bootstrap.min.js"></script>
	<script type="text/javascript" src="../../js/auth.js"></script>
</head>
<body>
<div class="container-fluid">
    <nav class="navbar navbar-default" role="navigation">
        <div class="container-fluid">
            <div class="navbar-header">
                <a class="navbar-brand" href="../index.html">蛋糕商城</a>
                <a class="navbar-brand" href="../../pages/index.html">集成后台</a>
            </div>
            <div>
                <ul class="nav navbar-nav">
                    <li ><a href="../../custpages/admin/order_list.html">订单管理</a></li>
                    <li ><a href="../../custpages/admin/user_list.html">客户管理</a></li>
                    <li ><a href="../../custpages/admin/goods_list.html">商品管理</a></li>
                    <li ><a href="../../custpages/admin/type_list.html">类目管理</a></li>
                    <li><a href="javascript:cookieShopLogout()">退出</a></li>
                </ul>
            </div>
        </div>
    </nav>
   	<br>

	<div>
		<form class="form-inline" method="post" action="">
			<input type="text" class="form-control" id="nameAdd" name="name" placeholder="输入类目名称" required="required" style="width: 500px">
			<input type="button" class="btn btn-warning" value="添加类目" onclick="addGoodsType()"/>
		</form>
	</div>
	<br/>
	<table class="table table-bordered table-hover">
		<tr id="dghead">
			<th width="5%">ID</th>
			<th width="10%">名称</th>
			<th width="10%">操作</th>
		</tr>
		<tbody id="dgcontent">
		</tbody>
	</table>
</div>


<!-- 模态框 -->
<div class="modal fade" id="editModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <!-- 模态框头部 -->
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">模态标题</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <!-- 模态框主体 -->
      <div class="modal-body">
          <form class="form-horizontal" action="" method="post">
      		<input type="hidden" name="id" id="id">
      		<div class="form-group">
     			<label for="input_name" class="col-sm-4 control-label">类目名称</label>
     			<div class="col-sm-6">
        				<input type="text" class="form-control" id="name" name="name" value="" required="required">
     			</div>
      		</div>
       	</form>
      </div>
      <!-- 模态框底部 -->
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
        <button type="button" class="btn btn-primary" onclick="updateGoodsType()">提交保存</button>
      </div>
    </div>
  </div>
</div>
</body>
<script type="text/javascript">
$(document).ready(function(){
  checkAdminAccess();
  setTimeout(function() {
    loadDataGrid();
  }, 1000);
});

function loadDataGrid(){
	$.ajax({
		type:"post",
		url: "../../custcontroller/adminGoodsTypeCustController/goods_type_list",
		dataType: 'json',
		success: function(data, textStatus) {
			debugger;
			if ( data.rows !=null ){
			 for (let i=0;i<data.rows.length;i++){
				  appendGridRow(data.rows[i]);
				}
			}
		},
		complete : function(XMLHttpRequest, textStatus) {
		},
		error : function(XMLHttpRequest,textStatus,errorThrown) {
			alert("Error:"+textStatus);
			alert(errorThrown.toString());
		}
	});
}

function appendGridRow(row){
  var rowStr = "<tr id='girdrow'>\n"+
                "<td><p>"+row.id+"</p></td>\n"+
                "<td><p>"+row.name+"</p></td>\n"+
				genOperationTd(row) +
                "</tr>\n";
  $("#dgcontent").append(rowStr);
}

function showCoverImage(row){
	if(row.cover){
		return "<img style='height:100px;' border='1' src='data:image/png;base64,"+row.cover+"'/>";
	} else {
		return "<img style='height:100px;' border='1' src='../../images/blank.jpg'/>";
	}
}

function genOperationTd(row){
	var tdStr =	"<td nowrap>\n"+
	            "<a class=\"btn btn-primary\" href=\"javascript:showEditGoodsType("+row.id+")\">修改</a>\n"+
				"<a class=\"btn btn-danger\" href=\"javascript:softDeleteGoodsType("+row.id+")\">软删除</a>\n"+
				"<a class=\"btn btn-danger\" href=\"javascript:deleteGoodsType(id="+row.id+")\">删除</a>\n"+
				"</td>\n";
	return tdStr;
}

function showEditGoodsType(id){
	$.ajax({
		type:"post",
		url: "../../custcontroller/adminGoodsTypeCustController/show_edit_type",
		data: JSON.stringify({
		    id:id
		}),
		dataType: 'json',
		contentType:"application/json;charset=UTF-8",
		success: function(data, textStatus) {
			debugger;
			if ( data.success == true && data.rows !=null ){
			    $("#id").val(data.rows.id);
				$("#name").val(data.rows.name);
				$("#editModal").modal("show");
			}
		},
		complete : function(XMLHttpRequest, textStatus) {
		},
		error : function(XMLHttpRequest,textStatus,errorThrown) {
			alert("Error:"+textStatus);
			alert(errorThrown.toString());
		}
	});
}

function updateGoodsType(){
	$.ajax({
		type:"post",
		url: "../../custcontroller/adminGoodsTypeCustController/updateGoodsType",
		data: JSON.stringify({
		    id:parseInt($("#id").val()),
			name:$("#name").val(),
		}),
		dataType: 'json',
		contentType:"application/json;charset=UTF-8",
		success: function(data, textStatus) {
			debugger;
			if ( data.success == true){
			   window.location.reload();
			}
		},
		complete : function(XMLHttpRequest, textStatus) {
		},
		error : function(XMLHttpRequest,textStatus,errorThrown) {
			alert("Error:"+textStatus);
			alert(errorThrown.toString());
		}
	});
}

function deleteGoodsType(id){
	$.ajax({
		type: "post",
		url: "../../controller/goodsTypeController/deleteGoodsType",
		dataType: 'json',
		data:{"id":id},
		success: function(data, textStatus) {
		  window.location.reload();
		},
		complete : function(XMLHttpRequest, textStatus) {
		},
		error : function(XMLHttpRequest,textStatus,errorThrown) {
			alert("Error:"+textStatus);
			alert(errorThrown.toString());
		}
	});
}

function softDeleteGoodsType(id){
	$.ajax({
		type: "post",
		url: "../../controller/goodsTypeController/softDeleteGoodsType",
		data: {
			"id":id
		},
		dataType: 'json',
		success: function(data, textStatus) {
            window.location.reload();
		},
		complete : function(XMLHttpRequest, textStatus) {
		},
		error : function(XMLHttpRequest,textStatus,errorThrown) {
			alert("Error:"+textStatus);
			alert(errorThrown.toString());
		}
	});
}

function addGoodsType(){
	$.ajax({
		type: "post",
		url: "../../controller/goodsTypeController/addGoodsType",
    		data: JSON.stringify({
    		name:$("#nameAdd").val(),
    		active:true,
		}),
		dataType: 'json',
		contentType:"application/json;charset=UTF-8",
		success: function(data, textStatus) {
			if (data.success) {
				window.location.reload();
			}
			},
		complete : function(XMLHttpRequest, textStatus) {
		},
		error : function(XMLHttpRequest,textStatus,errorThrown) {
			alert("Error:"+textStatus);
			alert(errorThrown.toString());
		}
	});
}
</script>
</html>
